/**
 * @(#) AppmssProxy.as
 *
 * @date		2011. 03. 25
 * @version		1.0
 * @author		baek kwang hyoun
 * @description	평가결과 Proxy - Proxy
 */
package com.skt.common_hr.appess.model
{
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.wcl.dataloader.*;
	import com.wcl.utils.DateUtil;
	import com.wcl.utils.NumberUtil;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;

	public class AppessProxy extends CommonProxy
	{
		public static const NAME:String = "MedicineProxy";

		public static const LOAD_MEDICALMONEY_LIST_COMPLETE:String	= "LoadMedicalMoneyListComplete";
		public static const LOAD_MEDICALMONEY_LIST_FAILED:String	= "LoadMedicalMoneyListFailed";


		public var mainList:ArrayCollection;
		public var mainList2:ArrayCollection;
		public var mainList3:ArrayCollection;

		public var subTabList:ArrayCollection;
		public var acOitab:ArrayCollection;

		public var key:String;

		//지원기준
		public var loadFaultMessage:String;

		/**
		 * 생성자
		 *
		 * @param
		 * @return void
		 */
		public function AppessProxy(data:Object=null)
		{
			super(NAME, data);
		}

		// tab1
		public function loadList(I_PERNR:String, setFunc:Function):void
		{
			var obj:Object = {};
			obj.I_PERNR = I_PERNR;
			obj.I_MTYPE = 'A2';

			loadData("Z_HR_I001", obj, loadListResult, listFault, setFunc);
		}
		public function loadListResult(event:ResultEvent):void
		{
			if(retCode==0) {
				mainList = event.result.OTAB;
				event.result.func(mainList);
			}
			else {
				//Alert.show(retText);
				mainList = new ArrayCollection();
				event.result.func(mainList);
			}
		}


		// tab2
		public function loadList2(I_PERNR:String, setFunc:Function):void
		{
			var obj:Object = {};
			obj.I_PERNR = I_PERNR;
			obj.I_MTYPE = 'A3';

			loadData("Z_HR_I001", obj, loadList2Result, listFault, setFunc);
		}
		public function loadList2Result(event:ResultEvent):void
		{
			var tabCheck:Object;
			if(event.result.E_BEGDA != null) {
				var begda:Date = DateUtil.stringToDateTime(event.result.E_BEGDA);
				var endda:Date = DateUtil.stringToDateTime(event.result.E_ENDDA);
				tabCheck = {};
				tabCheck.begda = begda;
				tabCheck.endda = endda;
			}
										
			if(retCode==0) {
				mainList2 = event.result.OTAB;
				event.result.func(mainList2, tabCheck);
			}
			else {
				//Alert.show(retText);
				mainList2 = new ArrayCollection();				
				event.result.func(mainList2, tabCheck);
			}
		}


	// tab3
		public function loadList3(I_PERNR:String, setFunc:Function):void
		{
			var obj:Object = {};
			obj.I_PERNR = I_PERNR;
			obj.I_MTYPE = 'A4';

			loadData("Z_HR_I001", obj, loadList3Result, listFault, setFunc);
		}
		
		
		public function loadList3Result(event:ResultEvent):void
		{
			var tabCheck:Object;
			if(event.result.E_BEGDA != null) {
				var begda:Date = DateUtil.stringToDateTime(event.result.E_BEGDA);
				var endda:Date = DateUtil.stringToDateTime(event.result.E_ENDDA);
				tabCheck = {};
				tabCheck.begda = begda;
				tabCheck.endda = endda;
			}
										
			if(retCode==0) {
				mainList3 = event.result.OTAB;
				event.result.func(mainList3, tabCheck);
			}
			else {
				//Alert.show(retText);
				mainList3 = new ArrayCollection();				
				event.result.func(mainList3, tabCheck);
			}
		}





















		// 부서 treedata
		public function loadTree(pernr:String, auth:String, setFunc:Function):void
		{
			var param:Object = new Object();
			param["I_PERNR"] = pernr;
			param["I_AUTH"] = auth;

			loadData("Z_HR_0015", param, loadTreeResult, listFault, setFunc);
		}
		public function loadTreeResult(event:ResultEvent):void
		{
			if(retCode==0) {
				var orgList:XMLList = parseOrgList(event.result.RTAB);
				event.result.func(orgList)
			}
			else {
				Alert.show('loadTreeResult err: ' + retText);
			}
		}



		private function parseOrgList(retObj:ArrayCollection):XMLList
		{
			var prevArr:Array = new Array();
			var root:XML;
			if(retObj.length>0) {
				root = <node label={retObj[0].STEXT} objid={retObj[0].OBJID} level={retObj[0].LEVEL} parent={retObj[0].UPPERORG}/>;
				prevArr[root.@level] = root;

				for(var i:int=1; i<retObj.length; i++)
				{
					var rec:Object = retObj[i];
					var node:XML = <node label={rec.STEXT} objid={rec.OBJID} level={rec.LEVEL} parent={rec.UPPERORG}/>;
					if(prevArr[parseInt(rec.LEVEL)-1]!=null) {
						XML(prevArr[parseInt(rec.LEVEL)-1]).appendChild(node);
					}
					prevArr[node.@level] = node;
				}
			}
			else {
				root = <node label="No Data" objid="0000" level="0" parent="0000"/>;
			}
			return new XMLList(root);
		}






















		// 가족관계
		public var familyList:ArrayCollection;
		public function loadCommonCode(code:String, setFunc:Function):void
		{
			var params:Object = new Object();
			params["I_DNAME"] = code;
			loadData("Z_HR_0003", params, loadCommonCodeResult, listFault, setFunc);
		}
		public function loadCommonCodeResult(event:ResultEvent):void
		{
			if(retCode==0) {
				var ac:ArrayCollection = event.result.ITAB;
				var obj:Object = {};
				obj.TEXT = "전체"
				obj.CODE = "";
				ac.addItemAt(obj, 0);
				event.result.func(ac);
			}
			else {
				Alert.show('loadCommonCodeResult err: ' + retText);
			}
		}


		// 리스트 업데이트
 /* 		public function detailList(selecteItem:Object, setFunc:Function):void
		{
			var params:Object = new Object();
			params["I_BUKRS"] = selecteItem.BUKRS;
			params["I_PERNR"] = selecteItem.PERNR;
			params["I_REQDT"] = selecteItem.REQDT_JSON;
			params["I_REQRN"] = selecteItem.REQRN;

			loadData("Z_HR_0142", params, detailListResult, listFault, setFunc);
		}
		public function detailListResult(event:ResultEvent):void
		{
			if(retCode==0) {
				subTabList 	= parseMainList(event.result.O_ITAB);
				acOitab		= parseSubList(event.result.O_FAM);

				var obj:Object = {};
				obj.acOitab		= subTabList;
				obj.acOfam	 	= acOitab;

				event.result.func(obj)
			}
			else {
				Alert.show('detailListResult err: ' + retText);
			}
		} */


		// 삭제
 		public function deleteList(selecteItem:Object, setFunc:Function):void
		{
			var params:Object = new Object();
			params["I_BUKRS"] = selecteItem.BUKRS;
			params["I_PERNR"] = selecteItem.PERNR;
			params["I_REQDT"] = selecteItem.REQDT_JSON;
			params["I_REQRN"] = selecteItem.REQRN;

			loadData("Z_HR_0146", params, deleteListResult, listFault, setFunc);
		}
		public function deleteListResult(event:ResultEvent):void
		{
			if(retCode==0) {
				event.result.func();
			}
			else {
				Alert.show('detailListResult err: ' + retText);
			}
		}

		// 재발급 사유 코드
		public var acReReqRsnCode:ArrayCollection;
		public function getReReqRsnCode(setFunc:Function):void
		{
			var params:Object = new Object();
			params["I_DNAME"] = "ZDWHYRE";
			loadData("Z_HR_0003", params, getReReqRsnCodeResult, listFault, setFunc);
		}
		public function getReReqRsnCodeResult(event:ResultEvent):void
		{
			if(retCode==0) {
				var obj:Object = {CODE:"0", TEXT:"재발급사유를 선택하세요"};
				acReReqRsnCode = event.result.ITAB;
				acReReqRsnCode.addItemAt(obj, 0);
				event.result.func(acReReqRsnCode);
			}
			else {
				Alert.show('getReReqRsnCodeResult err: ' + retText);
			}
		}

		// 관계 코드조회
		public var acFamilyCode:ArrayCollection;
		public function getFamilyCode(setFunc:Function):void
		{
			var params:Object = new Object();
			loadData("Z_HR_0144", params, getFamilyCodeResult, listFault, setFunc);
		}
		public function getFamilyCodeResult(event:ResultEvent):void
		{
			if(retCode==0) {
				var obj:Object = {CODE:"0", ATEXT:"선택하세요", AUSPR:"0"};
				acFamilyCode = event.result.O_ITAB;
				acFamilyCode.addItemAt(obj, 0);
				event.result.func(acFamilyCode);
			}
			else {
				Alert.show('getFamilyCodeResult err: ' + retText);
			}
		}

		// 변경신청항목 코드조회
		public var acEditListCode:ArrayCollection;
		public function getEditListCode(setFunc:Function):void
		{
			var params:Object = new Object();
			params["I_DNAME"] = "ZDCWHAT";
			loadData("Z_HR_0003", params, getEditListCodeResult, listFault, setFunc);
		}
		public function getEditListCodeResult(event:ResultEvent):void
		{
			if(retCode==0) {
				var obj:Object = {CODE:"0", TEXT:"선택하세요"};
				acEditListCode = event.result.ITAB;
				acEditListCode.addItemAt(obj, 0);
				event.result.func(acEditListCode);
			}
			else {
				Alert.show('getEditListCodeResult err: ' + retText);
			}
		}

		// 피부양자 신고 구분
		public var acDependantsDvCode:ArrayCollection;
		public function getDependantsDvCode(setFunc:Function):void
		{
			var params:Object = new Object();
			params["I_DNAME"] = "ZDIETYP";
			loadData("Z_HR_0003", params, getDependantsDvCodeResult, listFault, setFunc);
		}
		public function getDependantsDvCodeResult(event:ResultEvent):void
		{
			if(retCode==0) {
				var obj:Object = {CODE:"0", TEXT:"선택하세요"};
				acDependantsDvCode = event.result.ITAB;
				acDependantsDvCode.addItemAt(obj, 0);
				event.result.func(acDependantsDvCode);
			}
			else {
				Alert.show('getDependantsDvCodeResult err: ' + retText);
			}
		}

		// 추가/제외사유
		public var acDependantsRsnCode:ArrayCollection;
		public function getDependantsRsnCode(setFunc:Function):void
		{
			var params:Object = new Object();
			params["I_DNAME"] = "ZDIERSN";
			loadData("Z_HR_0003", params, getDependantsRsnCodeResult, listFault, setFunc);
		}
		public function getDependantsRsnCodeResult(event:ResultEvent):void
		{
			if(retCode==0) {
				var obj:Object = {CODE:"0", TEXT:"선택하세요"};
				acDependantsRsnCode = event.result.ITAB;
				acDependantsRsnCode.addItemAt(obj, 0);
				event.result.func(acDependantsRsnCode);
			}
			else {
				Alert.show('getDependantsRsnCodeResult err : ' + retText);
			}
		}



		// 저장
		public var iCallIdx:uint=0;
		public var iCalledIdx:uint=0;
		public var iErrIdx:uint=0;
		public var retTextTotal:String="";
		public var _dgDataList:ArrayCollection;
		public var _setFunc:Function;
		public var _dv:String;

		public function saveOrderList(dgDataList:ArrayCollection, setFunc:Function, dv:String="I"):void
		{
			_dgDataList = dgDataList;
			_setFunc = setFunc;
			_dv = dv;
			var oSelectedItem:Object;

			for(var i:int=0; i<_dgDataList.length; i++) {
				oSelectedItem = {};
				oSelectedItem = _dgDataList.getItemAt(i);
				if(oSelectedItem.checked == true) { // 지시요청이라면..
					iCallIdx++;
				}
			}

			for(var i:int=0; i<_dgDataList.length; i++) {
				oSelectedItem = {};
				oSelectedItem = _dgDataList.getItemAt(i);
				if(oSelectedItem.checked == true) { // 지시요청이라면..
					oSelectedItem.checked = false;
					sendSave(oSelectedItem);
					break;
				}
			}
		}
		public function sendSave(oSelectedItem:Object):void
		{
			var tables:Object = new Object();
			var table:Array = new Array();
			var params:Object = new Object();
			params["I_PERNR"] 	= oSelectedItem.PERNR;		//사원번호
			params["I_DATUM"] 	= oSelectedItem.BEGDA_JSON;	//일자
			if(_dv=="I") { // 저장되어있는걸 수정하는 로직만 ord에는 들어있음
				params["I_WSTAT"] 	= "2";						// 지시
				params["I_MODE"] 	= "U";						//수정
			}
			else if(_dv=="MULTI") {
				params["I_WSTAT"] 	= "2";						// 지시
				params["I_MODE"] 	= "I";						//신규
				params["I_WTYPE"] 	= oSelectedItem.WTYPE;
				params["I_WWHAT"] 	= oSelectedItem.WWHAT;
			}

			if(_dv=="I" || _dv=="MULTI") {
				saveData("Z_HR_0148", params, tables, saveOrderListResult, listFault, _setFunc);
			} else {
				saveData("Z_HR_0150", params, tables, saveOrderListResult, listFault, _setFunc);
			}
		}
		public function saveOrderSingle():void
		{
			var oSelectedItem:Object;
			for(var i:int; i<_dgDataList.length; i++) {
				oSelectedItem = {};
				oSelectedItem = _dgDataList.getItemAt(i);
				if(oSelectedItem.checked == true) { // 지시요청이라면..
					oSelectedItem.checked = false;
					sendSave(oSelectedItem);
					break;
				}
			}
		}
		public function saveOrderListResult(event:ResultEvent):void
		{
			iCalledIdx++;
			if(retCode==0) {

			}
			else {
				iErrIdx++;
				retTextTotal = retTextTotal + retText + "\r\n";
			}

			if(iCallIdx == iCalledIdx) {
				if(iErrIdx > 0) {
					Alert.show(retTextTotal);
				}
				else {
					event.result.func();
				}

				// 초기화..
				iCallIdx = 0;
				iCalledIdx = 0;
				_dv	= "";
			}
			saveOrderSingle();

		}


		private function parseMainList(recArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();

			for each(var obj:Object in recArr){
				//var retObj:MedicalSubTabVO = new MedicalSubTabVO();
				var retObj:Object = new Object();
				retObj["checked"] = false;
				for(var field:String in obj) {
					if(field == "BEGDA") {
						retObj[field+'_DT'] = DateUtil.stringToDateTime(obj[field]);
						retObj[field+'_TX'] = DateUtil.toDefaultString(retObj[field+'_DT']);
						retObj[field+'_JSON'] = DateUtil.toBasicString(retObj[field+'_DT']);
						retObj[field] = obj[field];
					}
					else if(field == "BETRG") {
						retObj[field] = Math.round(obj[field]*100);
					}
					else if(field == "TOTAL" || field == "MEDIC" || field == "PREME" ||
							field == "ADJUS" || field == "ZAVER" ) {
						retObj[field] = NumberUtil.NumberFormat(Math.round(obj[field]*100));
					}
					else {
						retObj[field] = obj[field];
					}
				}
				retArr.addItem(retObj);
			}
			return retArr;
		}


		private function toJsonType(vo:Object):Object {
			var retObj:Object = new Object;
			for(var field:String in vo) {
				if(vo[field] is Date) {
					retObj[field] = DateUtil.toBasicString(vo[field]);
				}
				else {
					retObj[field] = vo[field];
				}
				trace(retObj[field]);
			}
			return retObj;
		}

		private function listFault(event:FaultEvent):void
		{
			Alert.show('HthIcProxy.as listFault err: ' + retText);
		}



	}
}